﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using RED_NET_Core.BLL;
using RED_NET_Core.Entity;
namespace RED_NET.ContentPage
{
    public partial class MyDailyList : System.Web.UI.Page
    {
        private static int  visitId;
        private static Users user;
        protected void Init()
        {
            Session["PageName"] = "DailyList";
            user = new Users();
            user.UserId = -1;
            if (Session["USER"] != null)
                user = (Users)Session["USER"];
           
           
            if (Request.QueryString["VISIT_ID"] != null)
                visitId = Int32.Parse(Request.QueryString["VISIT_ID"]);
            else
                Response.Redirect("~/ContentPage/Error/PageError.aspx");
            DataTable _dt = new DataTable();
            try
            {
                _dt = News_BLL.ShowDailyList(visitId, 0);

                GridView_Daily.DataSource = _dt;

                if (_dt.Rows.Count != 0)
                {
                    Label_NULL.Visible = false;
                    GridView_Daily.DataBind();
                }
                else
                {
                    Label_NULL.Visible = true;
                }
            }
            catch (Exception ex)
            {
                Response.Redirect("~/ContentPage/Error/PageError.aspx");
            }
        }
        protected void GridView_Daily_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView_Daily.PageIndex = e.NewPageIndex;
            Init(); //重新绑定GridView数据的函数
            GridView_Daily.PageIndex = e.NewPageIndex;

        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //检测用户是否登录

                Init();
            }
        }
        protected void GridView_Daily_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[1].Visible = false; //如果想使第1列不可见,则将它的可见性设为false
                LinkButton LinkButton_Delete = (LinkButton)e.Row.FindControl("LinkButton_Delete");
                LinkButton_Delete.CommandArgument = e.Row.RowIndex.ToString();
                if (user.UserId== visitId||user.Right==3)
                {
                    LinkButton_Delete.Visible = true;
                }
                else
                {
                    LinkButton_Delete.Visible = false;
                }
            }
        }

        protected void GridView_Daily_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "delete")
            {
                int _index = Convert.ToInt32(e.CommandArgument);
                int _Id = Int32.Parse(GridView_Daily.Rows[_index].Cells[1].Text);
                try
                {
                    News_BLL.DeleteNews(_Id);
                }
                catch (Exception ex)
                {
                    Response.Redirect("~/ContentPage/Error/PageError.aspx");
                }
                if (user.Right == 3&&visitId!=user.UserId)
                {
                    News _news = News_BLL.ShowNews(_Id);
                    string title = "日志删除通知";
                    string _content = "很抱歉，您的日志:\"" + _news.Title + "\"由于涉及违规内容已被删除！";
                    int _num = 1;
                    int[] _sendto = new int[10];
                    _sendto[0] = visitId;
                    LoginBLL _login = new LoginBLL();
                    Users _user = _login.GetMessageOfUser(_news.UId);
                    string _sendtoStr = _user.NickName + ";";
                    News_BLL.AddLetter(user.UserId, _content, title, _sendto, _num, _sendtoStr);
                }
                Response.Redirect(Request.Url.ToString());
            }

        }

        protected void DataList_Daily_ItemDataBound(object sender, EventArgs e)
        {


        }

    }
}
